package org.hncors.gnssparse.fragement;

import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.southgnss.connect.TopDeviceManage;
import com.southgnss.gnssparse.GnssSateInfo;
import com.southgnss.gnssparse.GnssSateStatus;
import com.southgnss.gnssparse.GnssSateSysType;

import org.hncors.gnssparse.R;
import org.hncors.gnssparse.adapter.SatelliteItemAdapter;
import org.hncors.gnssparse.base.BaseFragment;
import org.hncors.gnssparse.model.SatelliteItemData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



public class GnssListFragment extends BaseFragment{
    private static final String TAG = "AlarmsFragment";
    private View view;
    private LocationManager mLocationManager;

    private ArrayList<SatelliteItemData> mSateItemsDataArray = new ArrayList<SatelliteItemData>();

    private SatelliteItemAdapter mSatelliteItemAdapter = null;
    private List<GnssSateInfo> mgpsSatsList = new ArrayList<>();
    private Comparator comparator;
    private Handler handler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //定义了一个地图view
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null){

            view = super.onCreateView(inflater,container,savedInstanceState);
            appendMainBody(this, R.layout.layout_setting_gnss_info_satellite_list);
            appendTopBody(R.layout.activity_top_icon);

            setTopBarTitle("星表");
            setTopLeftGone();

            handler = new Handler();
            handler.postDelayed(runnable,1000);
            initView(view);//初始化控件

        }
        return view;
    }

    //    控件初始化控件
    private void initView(View view) {
        ListView satelliteListView = (ListView) view.findViewById(R.id.listViewSatelliteList);
        if (satelliteListView != null) {
            mSatelliteItemAdapter = new SatelliteItemAdapter(mActivity, mSateItemsDataArray);
            satelliteListView.setAdapter(mSatelliteItemAdapter);
        }
    }

    @Override
    public void onDestroy() {
        Intent intent = new Intent();
        intent.putExtra("isClearSateListener", true);
        super.onDestroy();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                TopDeviceManage extras = TopDeviceManage.getInstance(null);
                int iSatInView = extras.getSatInView();
                if (mgpsSatsList == null) {
                    mgpsSatsList = new ArrayList<GnssSateInfo>();
                }

                boolean bSerialport = false;
                GnssSateInfo temSatellite = null;
                mgpsSatsList.clear();
                if (iSatInView > 0) {
                    int[] PrnList = extras.getPrnList();
                    float[] AzimuthList = extras.getAzimuthList();
                    float[] ElevationList = extras.getElevationList();
                    float[] Snr1List = extras.getSnr1List();
                    float[] Snr2List = extras.getSnr2List();
                    float[] Snr3List = extras.getSnr3List();
                    int[] StatusList = extras.getStatusList();
                    int[] SateSysList = extras.getSateSysListt();
                    /**
                     *
                     */
                    int nSnr = -1;
                    for (int i = 0; i < iSatInView; i++) {
                        temSatellite = new GnssSateInfo();
                        temSatellite.setPrn(PrnList[i]);
                        temSatellite.setAzimuth((int) AzimuthList[i]);
                        temSatellite.setElevation((int) ElevationList[i]);
                        temSatellite.setSnr1(Snr1List[i]);
                        temSatellite.setSnr2(Snr2List[i]);
                        temSatellite.setSnr3(Snr3List[i]);
                        temSatellite.setStatus(GnssSateStatus.swigToEnum(StatusList[i]));
                        if (bSerialport) {
                            nSnr = (int) temSatellite.getPrn();
                            if (nSnr >= 1 && nSnr <= 32) {
                                temSatellite.setType(GnssSateSysType.SATSYS_GPS);
                            } else if (nSnr >= 120 && nSnr <= 140) {
                                temSatellite.setType(GnssSateSysType.SATSYS_SBAS);
                            } else if (nSnr >= 38 && nSnr <= 69) {
                                temSatellite.setType(GnssSateSysType.SATSYS_GLONASS);
                            } else if (nSnr >= 161 && nSnr <= 198) {
                                temSatellite.setType(GnssSateSysType.SATSYS_BD);
                            } else {
                                temSatellite.setType(GnssSateSysType.SATSYS_NULL);
                            }
                        } else {
                            temSatellite.setType(GnssSateSysType.swigToEnum(SateSysList[i]));
                        }
                        mgpsSatsList.add(temSatellite);
                    }
                }
                //排序
                Comparator comparator = new MyComparator();//重要部分
                Collections.sort(mgpsSatsList, comparator);

                // 卫星列表
                mSateItemsDataArray.clear();
                for (int i = 0; i < mgpsSatsList.size(); i++) {
                    temSatellite = mgpsSatsList.get(i);
                    SatelliteItemData item = new SatelliteItemData();
                    if (temSatellite.getType() == GnssSateSysType.SATSYS_GPS) {
                        item.mSatelliteNum = "G";
                    }
                    else if (temSatellite.getType() == GnssSateSysType.SATSYS_GLONASS) {
                        item.mSatelliteNum = "R";
                    }
                    else if (temSatellite.getType() == GnssSateSysType.SATSYS_BD) {
                        item.mSatelliteNum = "C";
                    }
                    else if (temSatellite.getType() == GnssSateSysType.SATSYS_SBAS) {
                        item.mSatelliteNum = "S";
                    }
                    else if (temSatellite.getType() == GnssSateSysType.SATSYS_GALILEO) {
                        item.mSatelliteNum = "E";
                    }
                    else if (temSatellite.getType() == GnssSateSysType.SATSYS_QZSS) {
                        item.mSatelliteNum = "J";
                    }
                    else {
                        item.mSatelliteNum = "";
                    }

                    item.mSatelliteNum += String.valueOf(temSatellite.getPrn());
                    item.mSatelliteSnrl = String.valueOf(temSatellite.getSnr1());
                    item.mSatelliteSnrl += ("/" + String.valueOf(temSatellite.getSnr2()));
                    item.mSatelliteSnrl += ("/" + String.valueOf(temSatellite.getSnr3()));
                    item.mSatelliteElevatingAngle = String.valueOf(temSatellite.getElevation());
                    item.mSatelliteAzimuthalAngle = String.valueOf(temSatellite.getAzimuth());
                    if (GnssSateStatus.SATSTA_RESOLVE == temSatellite.getStatus()) {
                        item.mSatelliteStatus = getResources().getString(R.string.setting_rtk_gnss_info_lock);
                    }
                    else {
                        item.mSatelliteStatus = getResources().getString(R.string.setting_rtk_gnss_info_enable);
                    }
                    mSateItemsDataArray.add(item);
                }
                mSatelliteItemAdapter.notifyDataSetChanged();
            } catch (Exception e) {

            }
            handler.postDelayed(runnable,1000);
        }
    };

    public class MyComparator implements Comparator<GnssSateInfo> {

        public int compare(GnssSateInfo s1, GnssSateInfo s2) {
            if(s1.getPrn() > s2.getPrn()){
                return 1;
            } else if(s1.getPrn() < s2.getPrn()) {
                return -1;
            }
            return 0;
        }
    }
}
